/*
 * DocsEditorInput.java
 * 
 * Copyright (c) 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dcarew.googledocs.editor;

import java.io.IOException;

import org.dcarew.googledocs.DocsPlugin;
import org.dcarew.googledocs.utils.DocsUtils;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.util.ServiceException;

/**
 * This class acts as a light-weight editor input for the DocsEditor class.
 * 
 * @author Devon Carew
 */
public class DocsEditorInput
	implements IEditorInput //, IPersistableElement
{
	private SpreadsheetEntry 	spreadsheet;
	private DocumentListEntry 	document;
	
	private String 				docID;
	private String 				docName;
	private String 				docTooltip;
	
	
	public DocsEditorInput(SpreadsheetEntry spreadsheet)
	{
		this.spreadsheet = spreadsheet;
	}
	
	public DocsEditorInput(DocumentListEntry document)
	{
		this.document = document;
	}
	
	public DocsEditorInput(String docID, String docName, String docTooltip)
	{
		this.docID = docID;
		this.docName = docName;
		this.docTooltip = docTooltip;
	}
	
	public SpreadsheetEntry getSpreadsheet()
	{
		return spreadsheet;
	}
	
	public DocumentListEntry getDocument()
	{
		return document;
	}
	
	public SpreadsheetEntry reconstituteSpreadsheet()
		throws IOException, ServiceException
	{
		spreadsheet = DocsPlugin.openSpreadSheet(getSpreadsheetID());
		
		return spreadsheet;
	}
	
	public String getSpreadsheetID()
	{
		if (spreadsheet != null)
			return spreadsheet.getId();
		else if (document != null)
			return document.getId();
		else
			return docID;
	}
	
	public String getSpreadsheetKey()
	{
		if (spreadsheet != null)
		{
			return spreadsheet.getKey();
		}
		else if (document != null)
		{
			return document.getKey();
		}
		else
		{
			return FeedURLFactory.getSpreadsheetKeyFromUrl(getSpreadsheetID());
		}
	}
	
	/**
	 * Returns whether the editor input exists.
	 * <p>
	 * This method is primarily used to determine if an editor input should appear in the "File Most
	 * Recently Used" menu. An editor input will appear in the list until the return value of
	 * <code>exists</code> becomes <code>false</code> or it drops off the bottom of the list.
	 * 
	 * @return <code>true</code> if the editor input exists; <code>false</code> otherwise
	 */
	public boolean exists()
	{
		return spreadsheet != null || document != null;
	}
	
	public ImageDescriptor getImageDescriptor()
	{
		if (spreadsheet != null)
			return DocsPlugin.getImageDescriptor("icons/spreadsheet.gif");
		else
			return DocsPlugin.getImageDescriptor("icons/document.gif");
	}
	
	public String getName()
	{
		if (spreadsheet != null)
			return spreadsheet.getTitle().getPlainText();
		else if (document != null)
			return document.getTitle().getPlainText();
		else
			return docName;
	}
	
	public IPersistableElement getPersistable()
	{
		return null;
	}
	
//	// IPersistableElement
//	
//	public IPersistableElement getPersistable()
//	{
//		return this;
//	}
//	
//	public String getFactoryId()
//	{
//		return DocsElementFactory.FACTORY_ID;
//	}
//	
//	public void saveState(IMemento memento)
//	{
//		DocsElementFactory.saveState(this, memento);
//	}
	
	public String getToolTipText()
	{
		if (spreadsheet != null)
			return DocsUtils.describeSpreadsheet(spreadsheet);
		else if (document != null)
			return DocsUtils.describeDocument(document);
		else
			return docTooltip;
	}
	
	public int hashCode()
	{
		return spreadsheet.getKey().hashCode();
	}
	
	public boolean equals(Object obj)
	{
		if (!(obj instanceof DocsEditorInput))
			return false;
		
		DocsEditorInput other = (DocsEditorInput)obj;
		
		return other.getSpreadsheetKey().equals(getSpreadsheetKey());
	}
	
	public String toString()
	{
		return getClass().getName() + "(" + getName() + ")";
	}
	
	public Object getAdapter(final Class adapterType)
	{
		return Platform.getAdapterManager().getAdapter(this, adapterType);
	}

}
